package com.achievo.know.application.impl;

import javax.naming.NamingException;
import javax.naming.directory.Attributes;

import org.apache.commons.lang.StringUtils;
import org.springframework.ldap.core.AttributesMapper;

import com.achievo.know.domain.User;

public class UserAttributesMapper implements AttributesMapper {

	@Override
	public Object mapFromAttributes(Attributes attrs) throws NamingException {
		 User person = new User();
         if(attrs.get("cn")!=null)
        	 person.setRealName((String) attrs.get("cn").get());
         if(attrs.get("mail")!=null){
        	 String email=(String) attrs.get("mail").get();
        	 person.setEmail(email);
        	 if(StringUtils.isNotEmpty(email)){
        		 person.setUsername((String) attrs.get("mailNickname").get());
        	 }
         }else{
        	 person.setUsername((String) attrs.get("cn").get());
         }
         if(attrs.get("department")!=null)
        	 person.setDeptment((String) attrs.get("department").get());
         return person;
	}

}
